
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

public class Conta extends Drawable {

    //Cor
    public static final int COR = 0xFF8000;
    //Fonte
    public static final Font DRAW_FONT = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);
    //Tipos de conta 
    public static final int DIVIDIR = 0;
    public static final int MULTIPLICAR = 1;
    public static final int SUBTRAIR = 2;
    public static final int SOMAR = 3;
    //Campos de conta
    public int valor1;
    public int tipo;
    public int valor2;
    public int resultado;
    //Entrada usuario
    public StringBuffer value = new StringBuffer();

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append(valor1);
        sb.append(" ");
        sb.append(getSinal());
        sb.append(" ");
        sb.append(valor2);
        sb.append(" = ");
        sb.append(resultado);
        return sb.toString();
    }

    public char getSinal() {
        char sinal = 0;
        switch (tipo) {
            case Conta.DIVIDIR:
                sinal = '/';
                break;
            case Conta.MULTIPLICAR:
                sinal = '*';
                break;
            case Conta.SUBTRAIR:
                sinal = '-';
                break;
            case Conta.SOMAR:
                sinal = '+';
                break;
        }
        return sinal;
    }

    public void draw(Graphics g) {
        //Pinta
        g.setColor(COR);
        g.setFont(DRAW_FONT);
        g.drawString(getDrawText(), (int) getX(), (int) getY(), 0);
    }

    public String getDrawText() {
        String drawText = valor1 + " " + getSinal() + " " + valor2 + " = " + value.toString();
        return drawText;
    }

    public int getWidth() {
        return DRAW_FONT.stringWidth(getDrawText());
    }

    public boolean verfica() {
        return value.toString().equals("" + resultado);
    }

    public void keyPressed(int keyCode) {
        if (value.length() < 3) {
            switch (keyCode) {
                case Canvas.KEY_NUM0:
                    if (value.length() > 0 && value.charAt(0) == '0') {
                        value.deleteCharAt(0);
                    }
                    value.append(0);
                    break;
                case Canvas.KEY_NUM1:
                    if (value.length() > 0 && value.charAt(0) == '0') {
                        value.deleteCharAt(0);
                    }
                    value.append(1);
                    break;
                case Canvas.KEY_NUM2:
                    if (value.length() > 0 && value.charAt(0) == '0') {
                        value.deleteCharAt(0);
                    }
                    value.append(2);
                    break;
                case Canvas.KEY_NUM3:
                    if (value.length() > 0 && value.charAt(0) == '0') {
                        value.deleteCharAt(0);
                    }
                    value.append(3);
                    break;
                case Canvas.KEY_NUM4:
                    if (value.length() > 0 && value.charAt(0) == '0') {
                        value.deleteCharAt(0);
                    }
                    value.append(4);
                    break;
                case Canvas.KEY_NUM5:
                    if (value.length() > 0 && value.charAt(0) == '0') {
                        value.deleteCharAt(0);
                    }
                    value.append(5);
                    break;
                case Canvas.KEY_NUM6:
                    if (value.length() > 0 && value.charAt(0) == '0') {
                        value.deleteCharAt(0);
                    }
                    value.append(6);
                    break;
                case Canvas.KEY_NUM7:
                    if (value.length() > 0 && value.charAt(0) == '0') {
                        value.deleteCharAt(0);
                    }
                    value.append(7);
                    break;
                case Canvas.KEY_NUM8:
                    if (value.length() > 0 && value.charAt(0) == '0') {
                        value.deleteCharAt(0);
                    }
                    value.append(8);
                    break;
                case Canvas.KEY_NUM9:
                    if (value.length() > 0 && value.charAt(0) == '0') {
                        value.deleteCharAt(0);
                    }
                    value.append(9);
                    break;
            }

        }

        if (keyCode == -8 || keyCode == Softkeys.rightSoftKey) {
            if (value.length() > 0) {
                value.deleteCharAt(value.length() - 1);
            }
        }
    }
}